package com.fengxu.springcloud.controller;

import com.fengxu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Log4j2
// 全局的fallback配置，几乎更@HystrixCommand一样
@DefaultProperties(defaultFallback = "global_fallback")
public class PaymentHystrixFeignController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/ok/{id}")
    @HystrixCommand // 一定要有这个注解才会实现服务降级!!!
    String payment_ok(@PathVariable("id") Long id){
        int a = 1 / 0;
        return paymentHystrixService.payment_ok(id);
    }

    @GetMapping("/timeout")
    @HystrixCommand(fallbackMethod = "payment_timeout_fallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
    })
    String payment_timeout(){
        return paymentHystrixService.payment_timeout();
    }

    // payment_timeout的服务降级方法!
    String payment_timeout_fallback(){
        return "我是消费者80，尊敬的客户,您的请求超时请稍后再试!";
    }

    // 全局的默认fallback方法需要用@DefaultProperties(defaultFallback = "global_fallback")指定
    String global_fallback(){
        return "尊敬的客户您暂时不能访问此服务，请稍后再试!";
    }
}
